import React from 'react'

import * as AssetByIdAction  from '../actions/AssetByIdAction';
import { connect }  from 'react-redux';

class WechatImage extends React.Component {
  constructor(props) {
    super(props)
  }

  componentDidMount() {
    var self = this;
    var media_id = this.props.media_id
    // var appId = self.props.wechat.length > 0?self.props.wechat[0].appId:''
    // console.log(appId)
    if (media_id && media_id.length > 0) {
      // console.log(media_id)
      this.props.dispatch(
        AssetByIdAction.loadAssetImageByMediaIdAsync({
          media_id: media_id,
          type: 'image'
        })
      )
    }
  }

  componentDidUpdate() {
    // console.log(this.props.asset)
  }

  render() {
    var url = "";
    if (this.props.asset && this.props.asset[this.props.media_id]) {
      // console.log(this.props.asset[this.props.media_id])
      url = this.props.asset[this.props.media_id].data
      // console.log(url)
    }
    return (
      <img src={url}/>
    )
  }
}

const mapStateToProps = (state, ownProps) => {
  // console.log(state)
  return { asset: state.asset }
}

export default connect(mapStateToProps)(WechatImage);
